package com.jlastu.dlxy_androidapp.entity;

import com.jlastu.dlxy_androidapp.entity.Order;
import com.jlastu.dlxy_androidapp.entity.Product;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public class OrderItem {
    private Long id; // 订单项ID
    private Order order; // 关联的订单对象
    private Product product; // 订单项中的商品对象
    private int quantity; // 商品数量
    private BigDecimal price; // 商品单价
    private BigDecimal subtotal; // 小计（数量*单价）
    private BigDecimal discount; // 折扣金额，默认为0

    private LocalDateTime createTime; // 创建时间
    private LocalDateTime updateTime; // 更新时间
    private double itemTotal;

    // 构造函数
    public OrderItem() {
    }

    public OrderItem(Order order, Product product, int quantity, BigDecimal price) {
        this.order = order;
        this.product = product;
        this.quantity = quantity;
        this.price = price;
        this.subtotal = price.multiply(BigDecimal.valueOf(quantity));
        this.createTime = LocalDateTime.now();
    }


    // Getter 和 Setter 方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
        updateSubtotal();
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
        updateSubtotal();
    }

    public BigDecimal getSubtotal() {
        return subtotal;
    }

    // 更新小计的辅助方法
    private void updateSubtotal() {
        this.subtotal = price.multiply(BigDecimal.valueOf(quantity));
    }

    public BigDecimal getDiscount() {
        return discount;
    }

    public void setDiscount(BigDecimal discount) {
        this.discount = discount;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    // 重写toString方法，便于打印和查看对象状态
    @Override
    public String toString() {
        return "OrderItem{" +
                "id=" + id +
                ", order=" + order +
                ", product=" + product +
                ", quantity=" + quantity +
                ", price=" + price +
                ", subtotal=" + subtotal +
                ", discount=" + discount +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                '}';
    }

    public double getItemTotal() {
        return itemTotal;
    }
}
